package com.atf.atfconfigurator.views;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.configuration.PropertiesConfiguration;

import com.atf.atfconfigurator.AtfconfiguratorUI;
import com.atf.atfconfigurator.composite.CollapseableBox;
import com.atf.atfconfigurator.utils.WasConstant;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.NativeSelect;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class ThreadPoolView extends VerticalLayout implements View {
	private PropertiesConfiguration wasConfigProperty;
	private List<String> threadPoolParaList= Arrays.asList("Name", "MinimumSize", "MaximumSize","InactivityTimeout","Description","IsGrowable");
	private List<String> availableThreadPools=new ArrayList<String> ();
	@Override
	public void enter(ViewChangeEvent event) {
		// TODO Auto-generated method stub
		
		wasConfigProperty=((AtfconfiguratorUI) getUI()).getWasConfiguration();	
		setSpacing(true);
        setMargin(true);

        Label title = new Label(wasConfigProperty.getString("appName"));
        title.addStyleName("h2");
        title.addStyleName("colored");
        addComponent(title);
        
		final FormLayout form = new FormLayout();
        form.setMargin(false);
        form.setSpacing(true);
        form.setWidth("100%");
        //form.set
        form.addStyleName("light");
        addComponent(form);
        
        
		 final TextField noOfthreadPool = new TextField("Number of ThreadPool");
	     int noOfthreadPoolValue=wasConfigProperty.getInt("noOfthreadPool",0);
	     noOfthreadPool.setValue(String.valueOf(noOfthreadPoolValue));
	     noOfthreadPool.setWidth("50%");
	     form.addComponent(noOfthreadPool);
	     
	     
	     noOfthreadPool.addValueChangeListener(new Property.ValueChangeListener(){

				/**
				 * 
				 */
				private static final long serialVersionUID = 1L;

				@Override
				public void valueChange(ValueChangeEvent event) {
					// TODO Auto-generated method stub
					((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("noOfthreadPool",  (String) event.getProperty().getValue());
					((AtfconfiguratorUI) getUI()).navigator.navigateTo("ThreadPool");
				}        	
	        });
	     List<Object> availableTreadPoolsName=WasConstant.getList(WasConstant.WAS_XML_THREADPOOL_NAME+".names");
	     for(Object th:availableTreadPoolsName){
	    	 availableThreadPools.add((String)th);
	     }
	     
	     if(noOfthreadPoolValue > 0){
	    	 for(int i=1; i<noOfthreadPoolValue+1; i++){
	    		 form.addComponent(new CollapseableBox(String.valueOf(i),getThreadPoolEntries(i), false));
	    	 }
	     }
        	
       
	}
	public FormLayout getThreadPoolEntries(final int pos){
		final FormLayout form = new FormLayout();
		//application Name
		form.setMargin(false);
        form.setSpacing(true);
        form.setWidth("100%");
        //form.set
        form.addStyleName("light");
        
        for(String par:threadPoolParaList){
        	String value=wasConfigProperty.getString("threadPool"+par+"."+pos, "");
        	if(par.equalsIgnoreCase("Name")){
        		NativeSelect select = new NativeSelect("Name");
        		form.addComponent(select);
        		select.setNullSelectionAllowed(false);
        		for(String th:availableThreadPools){
        			select.addItem(th);
       	     	}
        		if(value.equalsIgnoreCase("")){
        			select.setValue(select.getItemIds().iterator().next());
        		}else{
        			select.setValue(value);
        		}
        		
        	}else{
        		final TextField tempText = new TextField(par);
              	tempText.setValue(value);
    	      	tempText.setWidth("100%");
    	      	tempText.addValueChangeListener(new Property.ValueChangeListener(){
    	
    	   			@Override
    	   			public void valueChange(ValueChangeEvent event) {
    	   				// TODO Auto-generated method stub
    	   				String value = (String) event.getProperty().getValue();
    	   				((AtfconfiguratorUI) getUI()).getWasConfiguration().setProperty("threadPool"+tempText.getCaption()+"."+pos, value);
    	   			}
    	           	
    	           });
    	      	tempText.setImmediate(true);
                form.addComponent(tempText);
        	}
        	
        	 
        }
       
        return form;
	}
}
